<?php


require_once("../config/config.php");


class TrackingResponse
{
	var $orderId; // int
	var $location; // string
	
	//One of the strings "UNKNOWN_ID", "NOT_PICKED_UP", "IN_TRANSIT", or "DELIVERED".
	var $deliveryStatus;
	
	var $date; // DateTime() YYYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]  .. T is literal
			  // example being 2008-12-11T21:27:00Z-06:00
					
	function __construct($id, $loc, $stat, $d)
	{
		$this->orderId = $id;
		$this->location = $loc;
		$this->deliveryStatus = $stat;
		$this->date = $d;
	}
};

class TrackingService
{
	function track($orderID)
	{
		error_log(var_export($orderID, true));
		global $config;
		global $ICE;
		
		Ice_loadProfile();

		$proxy = $ICE->stringToProxy($config['CS430 Integration']['crm orb locator']);

		$crm = $proxy->ice_checkedCast("::CustomerRelationsManagement::CRM");
			
		try {
			$order = $crm->LoadOrderById(intval($orderID));
		}
		catch (Exception $e)
		{
			return new TrackingResponse(intval($orderID), "", "UNKNOWN_ID", date("c"));
		}
		
		if ($order->getStatus() == CustomerRelationsManagement_OrderStatus::DELIVERED)
		{
			try {
				
				$dl = $order->getDeadline();
				$iso8601dt = $dl->year . "-" . $dl->month . "-" . $dl->day . "T17:00:00Z-06:00";
				return new TrackingResponse($orderID, $order->getDst(), "DELIVERED", $iso8601dt);
			}
			catch (Exception $e)
			{
				return new TrackingResponse(intval($orderID), "", "ORDER_ERROR", date("c"));
			}
		}
		else
		{	// not delivered, we need to fetch the order status from
			// the tracking system
			try {
				$trackproxy = $ICE->stringToProxy($config['CS430 Integration']['track orb locator']);
				$tracking = $trackproxy->ice_checkedCast("::Tracking::TrackingService");
				
				$tracking_data = $tracking->getAllData();
				$status = "U"; // unknown
				for ($i = 0; $i < count($tracking_data->Trucks); $i++)
				{
					for ($j = 0; $j < count($tracking_data->Trucks[$i]->stops); $j++)
					{
						for ($k = 0; $k < count($tracking_data->Trucks[$i]->stops[$j]->actions); $k++)
						{
							if ($tracking_data->Trucks[$i]->stops[$j]->actions[$k]->orderID 
									== intval($orderID))
							{
								$action = $tracking_data->Trucks[$i]->stops[$j]->actions[$k];
								if ($action->type == Tracking_StopAction::PICKUP)
								{
									return new TrackingResponse(intval($orderID), 
																$tracking_data->Trucks[$i]->stops[$j]->location, 
																"NOT_PICKED_UP", 
																date("c"));
								}
								else
								{
									return new TrackingResponse(intval($orderID), 
																$tracking_data->Trucks[$i]->stops[0]->location, // NEXT location
																"IN_TRANSIT", 
																date("c"));
								}
								//Tracking_StopAction::PICKUP
							}
						}
						
					}
				}
				
			}
			catch (Exception $e) {
				return new TrackingResponse(intval($orderID), "", "TRACKING_ERROR", date("c"));
			}
		}
	}
};

$server = new SoapServer("CustomerQueryService.wsdl");
$server->setClass("TrackingService");
$server->handle();


?>